#!/usr/bin/env sh
#=============HEADER=========================================================|
#AUTHOR
#  Jefferson Rocha <lrcjefferson@gmail.com>
#
#DESCRIPTION
#
#LICENSE
# https://notabug.org/jeffersonrocha/BSO/raw/master/LICENSE
#============================================================================|
[ "$UID" -ne "0" ] && exit 1

#========LIBRARY======================|
. /etc/bso/bso.conf
#=====================================|

#========VARS=========================|
NAME="BSO - B.E.S.O.U.R.O"
VERSION="0.1"
ARCHIVE="/var/lib/bso/modules.txt"                                    #Modules.txt
LINK="https://notabug.org/jeffersonrocha/BSO/raw/master/modules.txt"  #Main Link
USE="bso"
#=====================================|

#=======TESTS=========================|
if [ -z "$DIRECTORY_INSTALL_MODULES" ] || [ "$DIRECTORY_INSTALL_MODULES" = '/' ]; then
      printf '%b' "VAR 'DIRECTORY_INSTALL_MODULES' NULL!\n"
      printf '%b' "Please, edit: /etc/bso/bso.conf\n"
      exit 1
else
     if [ ! -d "$DIRECTORY_INSTALL_MODULES" ]; then  # PUSH var in bso.conf!
         if ! mkdir "$DIRECTORY_INSTALL_MODULES" 1>/dev/null 2>&1; then
             printf '%b' "DIRECTORY ${DIRECTORY_INSTALL_MODULES} NOT CREATED...[ERROR]\n"
             exit 1
         else
             chmod 777 "$DIRECTORY_INSTALL_MODULES"
         fi
     fi
fi

#=====================================|

#=======FUNCTIONS=====================|
USAGE(){ # Help
cat <<EOF
$NAME - Version $VERSION

USAGE:
bso [OPTION] ModuleName

    update
       Update a Modules.txt, receive the last list of modules

    search
       Search a Module in list.

    install
       Install a Module in your Computer.

EOF
}

UPDATE(){ # Update modules.txt
    cd "/var/lib/${USE}"
    printf '%b' "\nWait...\n\n"
    if wget --quiet --show-progress --output-document="modules.txt" "$LINK"; then
      printf '%b' "\nModules Successfully updated...[OK]\n"
      return 0
    else
      printf '%b' "ERROR NO UPDATE MODULE...[ERROR]\n"
      exit 1
    fi
}

SEARCH(){
typeset PACK # Local only.

    # Search Module
    [ ! -e "$ARCHIVE" ] && { printf '%b' "${ARCHIVE} NOT EXIST! USE 'bso update'...\n"; exit 1 ;}
    PACK=$(grep -w "^${1}" "$ARCHIVE" | cut -d '|' -f '1,2' | sed 's/|/ - /')
    [ -z "${PACK}" ] && { printf '%b' "NO PATTERN MATCH!\n" ; exit 1 ;}  # Dont MATCH.
    printf '%b' "${PACK}\n"
}

INSTALL(){
# Vars Local Only.
typeset ONLY_NAME 
typeset PACK
typeset STATUS

    [ ! -e "$ARCHIVE" ] && { printf '%b' "${ARCHIVE} NOT EXIST! USE 'bso update'...\n"; exit 1 ;}
    ONLY_NAME="$1"  # Only name, not URL.
    PACK=$(grep -w "^${1}" "${ARCHIVE}" | cut -d '|' -f '3')       # Only URL
    [ -z "${PACK}" ] && { printf '%b' "NOT FOUND...\n" ; exit 1 ;} # Null? package not exist, exit.
    echo
    wget --quiet --show-progress                       \
       --timestamping                                  \
       --directory-prefix="$DIRECTORY_INSTALL_MODULES" \
       "$PACK" # URL
    STATUS="$?"
    if [ "$STATUS" = "0" ]; then
        printf '%b' "\n${ONLY_NAME} DOWNLOADED IN: ${DIRECTORY_INSTALL_MODULES}...[OK]\n"
        chmod 777 "${DIRECTORY_INSTALL_MODULES}/${ONLY_NAME}.bso"
    else
        printf '%b'"\n${ONLY_NAME} NOT DOWNLOAD...[ERRO]\n"
        exit 1
    fi
       
}
#=====================================|



case "$1" in
   update)  UPDATE ;;
   search)
        shift
        [ -z "$1" ] && USAGE # Null?
        SEARCH "$@"
   ;;
   install)
        shift
        [ -z "$1" ] && USAGE # Null?
        INSTALL "$@"
   ;;
   *) USAGE ;;
esac

